//
//  SecondViewController.m
//  KVO_Demo
//
//  Created by meitianhui2 on 2017/12/25.
//  Copyright © 2017年 DeLongYang. All rights reserved.
/*
    主要是测试看看自定义的KVO 能否引起内存泄漏
    1.0 如果添加了的话 没有移除就会有内存泄漏 已经确认
    2.0 如果我们重复移除 或者 重复添加是否会造成crash？  经过测试重复移除并不会crash  也不会内存泄漏
        也不会重复添加 针对某一个属性
    下面测试不通过  ！！！！
    3.0 我们还需要测试一个 基本数据类型 也就是 非指针类型 非对象类型   !!!  会出现错误 如果是基本类型
 
 */

#import "SecondViewController.h"
#import "NSObject+KVO.h"

@interface SecondMessage:NSObject

@property (nonatomic,copy)NSString *text;

@property (nonatomic,copy)NSString *secondText;

@property (nonatomic,assign)int age;

@end

@implementation SecondMessage

@end


@interface SecondViewController ()

@property (weak, nonatomic) IBOutlet UITextField *textField;

@property (strong,nonatomic)SecondMessage *message;

@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    [self secondRegisteCustomKVO];
//    
//    [self secondRegistTwo];
//    [self secondRegistTwo];
//    [self secondRegistTwo];
//    [self secondRemoveTwo];
    
    [self threeRegisteKVO];
    [self threeRemoveKVO];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc
{
//    NSString *key = NSStringFromSelector(@selector(text));
//    NSLog(@"key is %@",key);
//    [self.message PG_removeObserver:self forKey:key];
    
    NSLog(@"Second View Controller  dealloc ");
}

- (void)viewWillDisappear:(BOOL)animated
{
    // 如果不移除掉的话会造成 内存泄漏
    NSString *key = NSStringFromSelector(@selector(text));
    NSLog(@"key is %@",key);
    [self.message PG_removeObserver:self forKey:key];
}

#pragma mark ---- 我们测试如果不移除掉是否会造成 内存泄漏 
- (void)secondRegisteCustomKVO
{
    //
    if (!self.message) {
        self.message = [[SecondMessage alloc] init];
    }
    
    NSString *key = NSStringFromSelector(@selector(text));
    [self.message PG_addObserber:self forKey:key withBlock:^(id observingObject, NSString *observedKey, id oldValue, id newValue) {
        NSLog(@"%@ . %@ is now:%@",observingObject,observedKey,newValue);
        dispatch_async(dispatch_get_main_queue(), ^{
            self.textField.text = newValue;
        });
    }];
    
    [self onCustomKVOButtonClick:nil];
}


- (void)secondRegistTwo
{

    if (!self.message) {
        self.message = [[SecondMessage alloc] init];
    }
    NSString *key = NSStringFromSelector(@selector(secondText));
    [self.message PG_addObserber:self forKey:key withBlock:^(id observingObject, NSString *observedKey, id oldValue, id newValue) {
        NSLog(@"%@ . %@ is now:%@",observingObject,observedKey,newValue);
    }];
    
    NSArray *msgs = @[@"China", @"HuNan", @"ShaoYang",@"DongKou"];
    NSUInteger index = arc4random_uniform((u_int32_t)msgs.count);
    self.message.secondText = msgs[index];
}

- (void)secondRemoveTwo
{
    if (!self.message) {
        self.message = [[SecondMessage alloc] init];
    }
    NSString *key = NSStringFromSelector(@selector(secondText));
    [self.message PG_removeObserver:self forKey:key];
}

- (void)threeRegisteKVO
{
    //
    if (!self.message) {
        self.message = [[SecondMessage alloc] init];
    }
    
    NSString *key = NSStringFromSelector(@selector(age));
    [self.message PG_addObserber:self forKey:key withBlock:^(id observingObject, NSString *observedKey, id oldValue, id newValue) {
        NSLog(@"%@ . %@ is now:%@",observingObject,observedKey,newValue);
    }];
    
    int ageValue = arc4random()%10;
    self.message.age = ageValue;
    
}

- (void)threeRemoveKVO
{
    if (!self.message) {
        self.message = [[SecondMessage alloc] init];
    }
    
    NSString *key = NSStringFromSelector(@selector(age));
    [self.message PG_removeObserver:self forKey:key];
}


#pragma mark ---- 按钮点击事件

- (IBAction)onCustomKVOButtonClick:(id)sender
{
    NSArray *msgs = @[@"Hello World!", @"Objective C", @"Swift", @"De Long", @"Yang", @"www.apple.com", @"www.google.com"];
    NSUInteger index = arc4random_uniform((u_int32_t)msgs.count);
    self.message.text = msgs[index];
}

- (IBAction)onRepeatAddClick:(id)sender
{
    
    if (!self.message) {
         self.message = [[SecondMessage alloc] init];
    }
    
    NSString *key = NSStringFromSelector(@selector(text));
    [self.message PG_addObserber:self forKey:key withBlock:^(id observingObject, NSString *observedKey, id oldValue, id newValue) {
        NSLog(@"%@ . %@ is now:%@",observingObject,observedKey,newValue);
        dispatch_async(dispatch_get_main_queue(), ^{
            self.textField.text = newValue;
        });
    }];
}

- (IBAction)onRepeatRemoveClick:(id)sender
{
    NSString *key = NSStringFromSelector(@selector(text));
    NSLog(@"key is %@",key);
    [self.message PG_removeObserver:self forKey:key];
}


@end
